package com.qf.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author 常肖飞先生
 * @version V1.0
 * @Project day41_1
 * @Package ${PACKAGE_NAME}
 * @Description: ${TODO}
 * @Date 2022/5/11 14:06
 */
@WebServlet(name = "SessionServlet1",urlPatterns = "/session1")
public class SessionServlet1 extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //session基本操作
        //获取当前的session对象
        HttpSession session = request.getSession();
        System.out.println(session.hashCode());
        //获取session对象的id
        String id = session.getId();
        System.out.println(id);//38425CA734029C6B2EFB8988ADE1D4C7
        //session对象的最大生命时长（服务器端保存session对象的时间）1800 30分钟
        System.out.println(session.getMaxInactiveInterval());
        //获取session对象创建的时间  1652249861164
        System.out.println(session.getCreationTime());
        //获取session对象的最后被修改的时间  1652249861164
        System.out.println(session.getLastAccessedTime());

        //session 当做一次会话周期中的容器使用
        //session 中添加内容。
        session.setAttribute("uname","tom");

        response.getWriter().println("1");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
